package com.fwtai.service.web;

import com.fwtai.bean.PageFormData;
import com.fwtai.tool.IdBuilder;
import com.fwtai.tool.ToolClient;
import com.fwtai.web.StoreGoodsDao;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.CompletableFuture;

/**
 * 门店商品库存业务层
 * @作者 田应平
 * @版本 v1.0
 * @QQ号码 444141300
 * @Email service@fwtai.com
 * @创建日期 2024-04-15 17:15:24
 * @官网 <url>http://www.fwtai.com</url>
*/
@Service
public class StoreGoodsService{

  private final StoreGoodsDao storeGoodsDao;

  private final ThreadPoolTaskExecutor taskExecutor;

  public StoreGoodsService(final StoreGoodsDao storeGoodsDao,final ThreadPoolTaskExecutor taskExecutor){
    this.storeGoodsDao = storeGoodsDao;
    this.taskExecutor = taskExecutor;
  }

  public String queryById(final Long kid){
    return CompletableFuture.supplyAsync(()->{
      if(kid == null){
        return ToolClient.jsonValidateField();
      }
      return ToolClient.queryJson(storeGoodsDao.queryById(kid));
    },taskExecutor).join();
  }


  public String edit(final HttpServletRequest request){
    return CompletableFuture.supplyAsync(()->{
      final PageFormData formData = new PageFormData(request,5);
      final String p_kid = "kid";
      final String p_inventory = "inventory";
      final String validate = ToolClient.validateField(formData,p_kid,p_inventory);
      if(validate != null) return validate;
      final String len_inventory = formData.getString(p_inventory);
      if(len_inventory.length() > 15){
        return ToolClient.createJsonFail("库存数量字数太多");
      }
      final Integer v_inventory = formData.getInteger(p_inventory);
      if(v_inventory == null){
        return ToolClient.createJsonFail("库存数量不是有效的数字");
      }else{
        formData.put(p_inventory,v_inventory);
      }
      final long kid = formData.getLong(p_kid);
      final Long v = storeGoodsDao.queryExistById(kid);
      if(v == null){
        return ToolClient.createJsonFail("数据不存在,刷新重试");
      }
      formData.put(p_kid,kid);
      return ToolClient.executeRows(storeGoodsDao.edit(formData));
    },taskExecutor).join();
  }

  public String listData(final PageFormData formData){
    return CompletableFuture.supplyAsync(()->{
      try{
        if(ToolClient.dataTableMysql(formData) == null) return ToolClient.jsonValidateField();
        final CompletableFuture<List<HashMap<String,Object>>> listData = CompletableFuture.supplyAsync(() -> storeGoodsDao.getListData(formData),taskExecutor);
        final CompletableFuture<Integer> listTotal = CompletableFuture.supplyAsync(() -> storeGoodsDao.getListTotal(formData),taskExecutor);
        //CompletableFuture.allOf(listData,listTotal).join();//若出错取消注释即可
        return ToolClient.dataTableOK(listData.join(),listTotal.join());
      } catch (final Exception ignored){
        return ToolClient.dataTableException();
      }
    },taskExecutor).join();
  }

  //先通过手机号查询门店id,然后查询全部商品的id,然后根据传入的优惠券数量添加商品；忽略已存在的商品(同1个门店的商品和商品优惠券数量一致时则忽略)
  public String addStoreGoods(final String userName,final Integer coupon){
    return CompletableFuture.supplyAsync(()->{
      if(userName == null || coupon == null){
        return ToolClient.jsonValidateField();
      }
      final Long kid  = storeGoodsDao.getStoreId(userName);
      if(kid == null){
        return ToolClient.createJsonFail("门店不存在");
      }
      final List<Long> listGoods = storeGoodsDao.getListGoods();
      final ArrayList<HashMap<String,Object>> list = new ArrayList<>(listGoods.size());
      for(int i = listGoods.size() - 1; i >= 0; i--){
        final HashMap<String,Object> map = new HashMap<>(5);
        map.put("kid",new IdBuilder().getId121L());
        map.put("store_id",kid);
        map.put("goods_id",listGoods.get(i));
        map.put("inventory",0);
        map.put("coupon",coupon);
        list.add(map);
      }
      return ToolClient.executeRows(storeGoodsDao.addStoreGoods(list),"上架成功","商品已存在或上架失败");
    },taskExecutor).join();
  }
}